#include "login.h"
#include "ui_login.h"
#include "QDebug"
#include "QJsonDocument"
#include "forgetpassword.h"
#include "../MainProject/sqlitedatabase.h"
#include "../MainProject/loggermanager.h"
#include <QCryptographicHash>

// 初始化静态成员指针
Login* Login::instance = nullptr;

Login* Login::getInstance(QWidget *parent) {
    if (instance == nullptr) {
        instance = new Login(parent);
    }
    return instance;
}

// 将构造函数设为私有
Login::Login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Login)
    ,common(this)
{
    ui->setupUi(this);
    common.loadQssfromfile(":/qss/loginQSS.qss");
    this->setFixedSize(844, 372); // 设置宽度为844像素，高度为372像素
    ui->forgetLabel->setStyleSheet("QLabel { color: blue; text-decoration: underline; }");
    ui->forgetLabel->setGeometry(QRect(100, 100, 200, 50)); // 设置标签的位置和大小

    ui->registerLabel->setStyleSheet("QLabel { color: blue; text-decoration: underline; }");
    connect(ui->forgetLabel, &ClickableLabel::clicked, this, &Login::on_forgetButton_clicked);
    connect(ui->registerLabel, &ClickableLabel::clicked, this, &Login::on_registerButton_clicked);
}

Login::~Login()
{
    delete ui;
    instance = nullptr; // 释放实例
}

void Login::on_loginButton_clicked()
{
    QString username = ui->phoneLineEdit->text();
    QString passwords = ui->passwordLineEdit->text();
    QString password = hashPassword(passwords);
    QString role = ui->comboBox->currentText();
    role = (role == "用户登录") ? "user" : "admin";

    if(username.isEmpty()){
        common.createMessageBox("警告", "用户名不能为空");
        //QMessageBox::information(this, "警告", "用户名不能为空");
        Logger().logError("用户名为空");
        return;
    }

    if(passwords.isEmpty()){
        common.createMessageBox("警告", "密码不能为空");
        //QMessageBox::information(this, "警告", "密码不能为空");
        Logger().logError("密码输入为空");
        return;
    }

    if(!username.isEmpty() && !passwords.isEmpty()){
        //通过用户名来查询密码
        QString datapassword;
        QString datarole;
        SQLiteDatabase* db = SQLiteDatabase::getInstance();
        QString condition = QString("username = '%1'").arg(username);
        QJsonArray result = db->read("users", condition);
        qDebug().noquote() << QJsonDocument(result).toJson(QJsonDocument::Indented);

        for (const QJsonValue &value : result) {
            QJsonObject obj = value.toObject();
            datapassword = obj["password"].toString();
            datarole = obj["role"].toString();
            qDebug() << "Password:" << password;
        }

        if(datapassword == password && role == datarole){
            Logger().logInfo("登录成功");
            emit loginSuccess(); // 发送登录成功信号
            emit sendLogininformation(username,role);

            //管理员和用户
            this->close(); // 关闭登录窗口
        } else {
            common.createMessageBox("警告", "登录失败");
            //QMessageBox::warning(this, "警告", "登录失败");
            Logger().logError("登录失败");
        }
    }
}

void Login::on_registerButton_clicked()
{
    Reg* regWindow = new Reg();
    regWindow->show();
    this->hide();
}

void Login::on_forgetButton_clicked()
{
    ForgetPassword* forgetWidget = new ForgetPassword();
    forgetWidget->show();
}

QString Login::hashPassword(const QString &password)
{
    QCryptographicHash hasher(QCryptographicHash::Md5);
    hasher.addData(password.toUtf8());
    return hasher.result().toHex();
}
